package com.moxiao.yfjh.module.blind.controller.admin.album;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.album.vo.BlindMemberAlbumExcelVO;
import com.moxiao.yfjh.module.blind.controller.admin.album.vo.BlindMemberAlbumExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.album.vo.BlindMemberAlbumPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.album.vo.BlindMemberAlbumRespVO;
import com.moxiao.yfjh.module.blind.convert.album.BlindMemberAlbumConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.album.BlindMemberAlbumDO;
import com.moxiao.yfjh.module.blind.service.album.BlindMemberAlbumService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲会员相册")
@RestController
@RequestMapping("/blind/member-album")
@Validated
public class BlindMemberAlbumController {

    @Resource
    private BlindMemberAlbumService memberAlbumService;


    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲会员相册")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:member-album:delete')")
    public CommonResult<Boolean> deleteMemberAlbum(@RequestParam("id") Long id) {
        memberAlbumService.deleteMemberAlbum(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲会员相册")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:member-album:query')")
    public CommonResult<BlindMemberAlbumRespVO> getMemberAlbum(@RequestParam("id") Long id) {
        BlindMemberAlbumDO memberAlbum = memberAlbumService.getMemberAlbum(id);
        return success(BlindMemberAlbumConvert.INSTANCE.convert(memberAlbum));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲会员相册列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:member-album:query')")
    public CommonResult<List<BlindMemberAlbumRespVO>> getMemberAlbumList(@RequestParam("ids") Collection<Long> ids) {
        List<BlindMemberAlbumDO> list = memberAlbumService.getMemberAlbumList(ids);
        return success(BlindMemberAlbumConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲会员相册分页")
    @PreAuthorize("@ss.hasPermission('blind:member-album:query')")
    public CommonResult<PageResult<BlindMemberAlbumRespVO>> getMemberAlbumPage(@Valid BlindMemberAlbumPageReqVO pageVO) {
        PageResult<BlindMemberAlbumDO> pageResult = memberAlbumService.getMemberAlbumPage(pageVO);
        return success(BlindMemberAlbumConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲会员相册 Excel")
    @PreAuthorize("@ss.hasPermission('blind:member-album:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberAlbumExcel(@Valid BlindMemberAlbumExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlindMemberAlbumDO> list = memberAlbumService.getMemberAlbumList(exportReqVO);
        // 导出 Excel
        List<BlindMemberAlbumExcelVO> datas = BlindMemberAlbumConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲会员相册.xls", "数据", BlindMemberAlbumExcelVO.class, datas);
    }

}
